I'm a newbie for C++, I'm currently learning it with the book C++ How To Program 8e. In the section Separating Interface from Implementation they have given an example like this
GradeBook.h
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook( string );
void setCourseName( string );
string getCourseName();
void displayMessage();
private:
string courseName;
};
GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook( string name )
{
setCourseName( name );
}
void GradeBook::setCourseName( string name )
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
}
main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
cout << "gradeBook1 created for: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for: " << gradeBook2.getCourseName()
<< endl;
}
When I'm executing this with CodeBlocks I get an error with following info
D:\C++\SIFI\main.o:main.cpp|| undefined reference to `GradeBook::GradeBook(std::string)'|
D:\C++\SIFI\main.o:main.cpp|| undefined reference to `GradeBook::GradeBook(std::string)'|
D:\C++\SIFI\main.o:main.cpp|| undefined reference to `GradeBook::getCourseName()'|
D:\C++\SIFI\main.o:main.cpp|| undefined reference to `GradeBook::getCourseName()'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I'm an absolute newbie and please help.
hritikchaudhary :
For those who are using the plain GNU Fortran compiler, rather than some IDE such as CodeBlocks, this would be the command line:\n\ng++ main.cpp GradeBook.cpp -o main.x\n",
2020-04-23T13:13:34
vincentp :
Some Code::Blocks bug... Sometimes, your cpp files are not added to your project, you have to add it by yourself to your project. (something like \"file > add file > existing file\"?)",
2014-11-20T13:17:15